package com.psjj.privilege.web.servlet;

import com.psjj.privilege.po.Role;
import com.psjj.privilege.po.User;
import com.psjj.privilege.service.RoleService;
import com.psjj.privilege.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user.do")
public class UserController extends BasicServlet {
    private UserService userService=new UserService();
    private RoleService roleService=new RoleService();
    private static final String BASIC_URL="/WEB-INF/page/user/";
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //User user=userService.login(username,pwd);
        User user = userService.loginJoinRoles(username,pwd);
        if(user!=null){
            if(user.getStatus()==1){
                req.getSession().setAttribute("user",user);
                resp.sendRedirect(req.getContextPath()+"/main.do?method=mainUI");

            }else {
                req.setAttribute("error","用户已禁用，请联系管理员！！！");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }




        }else{
            req.setAttribute("error","用户名或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);

        }

    }

    private void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath()+"/login.jsp");



    }
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if(username == null || "".equals(username) || "null".equals(username)){
            username = "%%";


        }else{
            username = "%"+username+"%";

        }
        int currentPage=req.getParameter("currentPage") == null ?1:Integer.parseInt(req.getParameter("currentPage"));
        if(currentPage<1){
            currentPage=1;

        }
        int pageSize=10;
        int total = userService.getCount(username);
        int pageNum=(int)Math.ceil(total/(double)pageSize);
        if(currentPage>pageNum){
            currentPage=pageNum;
        }
        int begin = (currentPage-1)*10;
        List<User> list = userService.getPage(username,begin,pageSize);
        req.setAttribute("currentPage",currentPage);
        req.setAttribute("pageSize",pageSize);
        req.setAttribute("pageNum",pageNum);
        req.setAttribute("total",total);
        req.setAttribute("list",list);






        req.getRequestDispatcher(BASIC_URL+"table.jsp").forward(req,resp);

    }
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> roles = roleService.queryRoles();
        req.setAttribute("roles",roles);



        req.getRequestDispatcher(BASIC_URL+"add.jsp").forward(req,resp);

    }
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String[] rids = req.getParameterValues("rid");
        String phone = req.getParameter("phone");
        String pwd = req.getParameter("pwd");
        User user = new User();
        user.setUsername(username);
        user.setPhone(phone);
        user.setPwd(pwd);
        userService.addUserJoinRoles(user,rids);
        resp.sendRedirect(req.getContextPath()+"/user.do?method=list");




    }
    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid=Integer.parseInt(req.getParameter("uid"));
        User user = userService.queryUserByUid(uid);
        List<Role> roles = roleService.queryRolesJoinRoleStatus(uid);
        req.setAttribute("user",user);
        req.setAttribute("roles",roles);
        req.getRequestDispatcher(BASIC_URL+"update.jsp").forward(req,resp);




    }
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid=Integer.parseInt(req.getParameter("uid"));
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String[] rids = req.getParameterValues("rid");
        User user = new User();
        user.setUid(uid);
        user.setUsername(username);
        user.setPhone(phone);
        userService.editUserJoinRole(user,rids);
        resp.sendRedirect(req.getContextPath()+"/user.do?method=list");





    }
    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid=Integer.parseInt(req.getParameter("uid"));
        userService.del(uid);
        resp.sendRedirect(req.getContextPath()+"/user.do?method=list");

    }
    private void resetPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid=Integer.parseInt(req.getParameter("uid"));
        userService.resetPwd(uid);
        req.getSession().invalidate();
        resp.getWriter().println("<script>parent.location.href='"+req.getContextPath()+"/login.jsp';</script>");




       
    }

}
